package com.qxzn.module.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.UserRemindBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.page.model.HomeModel;
import com.qxzn.app.util.Constant;
import com.qxzn.app.util.SharePreferenceUtil;
import com.qxzn.module.home.R;
import com.qxzn.module.home.R2;
import com.qxzn.module.home.adapter.UserRemindListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class UserRemindListActivity extends BaseActivity implements MyInterface.GetUserRemindListListener, OnRefreshListener, OnLoadMoreListener, OnItemClickListener {

    @BindView(R2.id.lrv_remind_list)
    LRecyclerView lrvRemindList;
    @BindView(R2.id.view_empty)
    View viewEmpty;
    private UserRemindListAdapter userRemindListAdapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private HomeModel homeModel;
    private List<UserRemindBean> dataList = new ArrayList<>();
    private String username;
    private int page = 1;
    private int pageSize = 10;

    @Override
    protected int initLayout() {
        return R.layout.activity_user_remind_list;
    }

    @Override
    protected void initView() {
        lrvRemindList.setLayoutManager(new LinearLayoutManager(this));
        userRemindListAdapter = new UserRemindListAdapter(this, dataList);
        lRecyclerViewAdapter = new LRecyclerViewAdapter(userRemindListAdapter);
        lrvRemindList.setEmptyView(viewEmpty);
        lrvRemindList.setAdapter(lRecyclerViewAdapter);
        lrvRemindList.setOnRefreshListener(this);
        lrvRemindList.setOnLoadMoreListener(this);
        lRecyclerViewAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("定时提醒");
        iv_right.setVisibility(View.VISIBLE);
        iv_right.setOnClickListener(this);
        iv_right.setImageResource(R.mipmap.icon_add_home);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        username = getIntent().getStringExtra(Constant.PutExtraKey.USERNAME);
        homeModel = new HomeModel(this);
        getUserRemindList();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.iv_right) {
            startActivityForResult(new Intent(this, EditRemindActivity.class).putExtra(Constant.PutExtraKey.USERNAME, username), 1);
        }
    }

    private void getUserRemindList() {
        showProgress();
        JSONObject params = new JSONObject();
        params.put("device_id", SharePreferenceUtil.getDeviceId(this));
        params.put("username", username);
        params.put("page", page);
        params.put("pageSize", pageSize);
        homeModel.setGetUserRemindListListener(this);
        homeModel.getUserRemindList(params);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                getUserRemindList();
            } else if (requestCode == 2) {
                getUserRemindList();
            }
        }
    }

    @Override
    public void onGetUserRemindListSuccess(BaseBean<List<UserRemindBean>> baseBean) {
        lrvRemindList.refreshComplete(pageSize);
        hideProgress();
        if (baseBean != null) {
            if (page == 1) {
                dataList.clear();
            }
            List<UserRemindBean> list = baseBean.getData();
            if (list != null) {
                if (list.size() == 0) {
                    lrvRemindList.setNoMore(true);
                }
                dataList.addAll(list);
            }
            userRemindListAdapter.notifyDataSetChanged();
            lRecyclerViewAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onGetUserRemindListFailure() {
        lrvRemindList.refreshComplete(pageSize);
        hideProgress();
    }

    @Override
    protected void onDestroy() {
        homeModel.removeListener();
        super.onDestroy();
    }

    @Override
    public void onLoadMore() {
        if (dataList.size() % pageSize == 0) {
            page++;
            getUserRemindList();
        } else {
            lrvRemindList.setNoMore(true);
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        getUserRemindList();
    }

    @Override
    public void onItemClick(View view, int position) {
        UserRemindBean bean = dataList.get(position);
        startActivityForResult(new Intent(this, EditRemindActivity.class).putExtra("bean", bean), 2);
    }

    @OnClick(R2.id.view_empty)
    public void onViewClicked() {
        page = 1;
        getUserRemindList();
    }
}
